import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
import store from '@/store'
import Login from '@/views/Login'

import { log } from 'three'

Vue.use(VueRouter)

// 动态路由
const theFirstInputRule = {
  path: '/theFirstInput',
  name: 'theFirstInput',
  component: () => import('@/views/transport/theFirstInput')

}
const totalCheckRule = {
  path: '/totalCheck',
  name: 'totalCheck',
  component: () => import('@/views/transport/totalCheck')

}
const checkRule = {
  path: '/check',
  name: 'check',
  component: () => import('@/views/goods management/check')

}
const handleRule = {
  path: '/handle',
  name: 'handle',
  component: () => import('@/views/goods management/handle')

}
const eggRule = {
  path: 'egg',
  name: 'egg',
  component: () => import('@/views/warehouse/egg')

}
const fruitRule = {
  path: '/fruit',
  name: 'fruit',
  component: () => import('@/views/warehouse/fruit')

}
const meatRule = {
  path: '/meat',
  name: 'meat',
  component: () => import('@/views/warehouse/meat')

}
const processRule = {
  path: '/process',
  name: 'process',
  component: () => import('@/views/warehouse/process')

}
const chakanRule = {
  path: '/chakan',
  name: 'chakan',
  component: () => import('@/views/warehouse/chakan')

}
const chuliRule = {
  path: '/chuli',
  name: 'chuli',
  component: () => import('@/views/warehouse/chuli')

}
const check2Rule = {
  path: '/check2',
  name: 'check2',
  component: () => import('@/views/staff management/check2')

}
const handle2Rule = {
  path: '/handle2',
  name: 'handle2',
  component: () => import('@/views/staff management/handle2')
}
const BackupRule = {
  path: '/Backup',
  name: 'Backup',
  component: () => import('@/views/Technology/Backup')

}
const DefaultRule = {
  path: '/Default',
  name: 'Default',
  component: () => import('@/views/Technology/Default')

}
const RecoverRule = {
  path: '/Recover',
  name: 'Recover',
  component: () => import('@/views/Technology/Recover')

}
const outputRule = {
  path: '/output',
  name: 'output',
  component: () => import('@/views/transport/output')

}
const outputCheckRule = {
  path: '/outputCheck',
  name: 'outputCheck',
  component: () => import('@/views/transport/outputCheck')
}

/**
 * eslint报错
 * error Unnecessarily quoted property 'xxx' found quote-props
 * 表示 key没必要加引号 'table': tableRule => table: tableRule
 */
// 路由规则和字符串的映射关系
const ruleMapping = {
  theFirstInput: theFirstInputRule,
  totalCheck: totalCheckRule,
  check: checkRule,
  handle: handleRule,
  egg: eggRule,
  meat: meatRule,
  fruit: fruitRule,
  process: processRule,
  chakan: chakanRule,
  chuli: chuliRule,
  check2: check2Rule,
  handle2: handle2Rule,
  Backup: BackupRule,
  Default: DefaultRule,
  Recover: RecoverRule,
  output: outputRule,
  outputCheck: outputCheckRule
}

const routes = [
  {
    path: '',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login

  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/home',
        component: () => import('@/views/Home')
      }
    ]
  }

  // 没有找到页面刷新动态路由丢失问题的解决方法 先将404页面注释
  // {
  //   path: '*',
  //   component: () => import('@/views/404')
  // }
]

const router = new VueRouter({
mode:'hash',
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  console.log('to..'+to.path)
  
  if (to.path == '/login') {
    console.log('登录中......................')
    
    next()
  } else {
    console.log('登录失败......................')
    const token = localStorage.getItem('Authorization')
    console.log('token'+token)
    console.log('token'+!token)
    if (token==null) {
      next('/login')
    } else {
      next()
    }
  }
})

export function initDynamicRoutes () {
  console.log(router)
  // 根据二级权限 对路由规则进行动态的添加
  const currentRoutes = router.options.routes
  // currentRoutes[2].children.push()
  const rightList = store.state.rightList
  console.log(rightList)
  rightList.forEach(item => { // 如果是没有子路由的话 就直接添加进去 如果有子路由的话就进入二级权限遍历
    console.log(item, 'item-1')
    // if (item.path ) {
    //   console.log('fhgh');
    //   const temp = ruleMapping[item.path]
    //   // 路由规则中添加元数据meta
    //   temp.meta = item.rights
    //   currentRoutes[1].push(temp)
    //   console.log('werer');
    // }

    item.children.forEach(item => {
      // item 二级权限
      console.log(item, 'item-2')
      console.log(ruleMapping)
      const temp = ruleMapping[item.path]
      console.log(temp)
      // 路由规则中添加元数据meta
      // temp.meta = item.rights
      // console.log(temp);
      currentRoutes[2].children.push(temp)
      // console.log(currentRoutes[1].children.push(temp));
      console.log(currentRoutes[2].children.push(temp))
    })
  })
  console.log(currentRoutes)
  router.addRoutes(currentRoutes)
  console.log(router.addRoutes(currentRoutes))
}

export default router
